<template>
  <div class="wrapper">
    <!--头部-->
    <!-- index单独的header -->
    <div class='index_header'>
      <div class=location-text>
        <i class="fa fa-map-marker"></i>
        天津大学北洋园校区
        <i class="fa fa-caret-down"></i>
      </div>
    </div>
    <!--搜索框部分-->
    <div class="search">
      <div class="fixed" ref="fixedbox">
        <div class="input-group">
          <input v-model="searchContent" type="text" style="color: black; font-family: fangsong; font-size: 4vw; "
            class="form-control" placeholder="搜索饿团团商家、商品名称">
          <span class="input-group-text">
            <i @click="toSearch" class="fa fa-search"></i>
          </span>
        </div>
      </div>
    </div>
    <!--点餐分类部分-->
    <ul class="foodtype">
      <li @click="toBusinessList(1)">
        <img src="../assets/img/index/美食.svg">
        <p>美食</p>
      </li>
      <li @click="toBusinessList(2)">
        <img src="../assets/img/index/早餐.svg">
        <p>早餐</p>
      </li>
      <li @click="toBusinessList(3)">
        <img src="../assets/img/index/跑腿代购.svg">
        <p>跑腿代购</p>
      </li>
      <li @click="toBusinessList(4)">
        <img src="../assets/img/index/汉堡披萨.svg">
        <p>汉堡披萨</p>
      </li>
      <li @click="toBusinessList(5)">
        <img src="../assets/img/index/甜品饮品.svg">
        <p>甜品饮品</p>
      </li>
      <li @click="toBusinessList(6)">
        <img src="../assets/img/index/速食简餐.svg">
        <p>速食简餐</p>
      </li>
      <li @click="toBusinessList(7)">
        <img src="../assets/img/index/地方小吃.svg">
        <p>地方小吃</p>
      </li>
      <li @click="toBusinessList(8)">
        <img src="../assets/img/index/米粉面馆.svg">
        <p>米粉面馆</p>
      </li>
      <li @click="toBusinessList(9)">
        <img src="../assets/img/index/包子粥铺.svg">
        <p>包子粥铺</p>
      </li>
      <li @click="toBusinessList(10)">
        <img src="../assets/img/index/炸鸡炸串.svg">
        <p>炸鸡炸串</p>
      </li>
    </ul>


    <!--横幅广告-->
    <div class="banner">
      <h3>品质套餐</h3>
      <p>搭配齐全，吃得好</p>
      <a @click="toRecommend">立即抢购 &gt;</a>
    </div>
    <!--超级会员-->
    <div class="supermember">
      <div class="left">
        <img src="../assets/img/index/supermember.svg">
        <h3>超级会员</h3>
        <p>&#8226; 每月享超值权益</p>
      </div>
      <div class="right">
        <a @click="toSuper">立即开通 &gt;</a>
      </div>
    </div>
    <!--推荐商家-->
    <div class="recommend">
      <div class="recommend-line"></div>
      <p>推荐商家</p>
      <div class="recommend-line"></div>
    </div>
    <!--推荐方式-->
    <ul class="recommend-style">
      <li class="card">
        <div style="margin-left: 2.5vw">筛选<i class="fa fa-filter" /></div>
      </li>

      <li :class="{ focus: (sort == '评星降序' || sort == '评星升序') }" class=" dropdown-toggle" style="left:0;"
        data-bs-toggle="dropdown">{{ (sort == '销量降序' || sort == '销量升序') ? '评星排序' : sort }}</li>
      <ul class="dropdown-menu">
        <li><a @click="changeSort('评星降序')" class="dropdown-item">评星降序</a></li>
        <li><a @click="changeSort('评星升序')" class="dropdown-item">评星升序</a></li>
      </ul>
      <li :class="{ focus: (sort == '销量降序' || sort == '销量升序') }" class="dropdown-toggle" style="left:0;"
        data-bs-toggle="dropdown">{{ (sort == '评星降序' || sort == '评星升序') ? '销量排序' : sort }}</li>
      <ul class="dropdown-menu">
        <li><a @click="changeSort('销量降序')" class="dropdown-item">销量降序</a></li>
        <li><a @click="changeSort('销量升序')" class="dropdown-item">销量升序</a></li>
      </ul>


    </ul>

    <!-- 横线 -->
    <hr style="color: black; margin: 0;">

    <!--推荐商家列表-->
    <ul class="business">
      <li v-for="item in businessArr" @click="toBusinessInfo(item.businessId)">
        <img :src="item.businessImg">
        <div class="business-info">
          <div class="business-info-h">
            <h3>{{ item.businessName }}</h3>
            <div class="business-info-like">&#8226;</div>
          </div>

          <div class="business-info-star">
            <div class="business-info-star-left">
              <i v-for="n in 5"
                :class="item.star >= n ? 'fa fa-star' : (item.star >= (n - 0.5) ? 'fa fa-star-half-o' : 'fa fa-star-o')"></i>
              <p>{{ item.star }}
              <div v-show="item.star == 5.0">.0</div>月售{{ item.sales }}单
              </p>
            </div>

            <div class="business-info-star-right">
              蜂鸟专送
            </div>
          </div>
          <div class="business-info-delivery">
            <p>&#165;{{ item.starPrice }}起送 | &#165;{{ item.deliveryPrice }}配送</p>
          </div>
          <div class="business-info-explain">
            <div>{{ item.businessExplain }}</div>
          </div>
        </div>
      </li>
    </ul>

    <!--活动广告 -->
    <div id="activity">
      <img id="activity-img" :class="{ hide: isShowed == 'false' }" src="../assets/img/index/红包1.png" width="250"
        alt="红包">
      <div @click="closeActivity" id="activity-close-btn" :class="{ hide: isShowed == 'false' }">x</div>
      <pre id="activity-content" :class="{ hide: isShowed == 'false' }">
              新人活动
              注册即送200e币
              <button @click="toLogin" id="activity-enter-btn" type="button" class="btn" >前往登录</button>
            </pre>
    </div>

    <GoTopButton />
    <!--底部菜单-->
    <Footer></Footer>
  </div>
</template>

<script>
//导入共通组件
import Footer from '../components/Footer.vue';
import GoTopButton from '../components/GoTopButton.vue'

export default {
  name: "Index",
  data() {
    return {
      businessArr: [],
      isShowed: 'true',
      searchContent: '',
      sort: (this.$route.query.sort != null) ? this.$route.query.sort : '评星降序',
    }
  },
  created() {
    this.init();
    if (this.sort == '评星降序') {
      this.$axios.post('BusinessController/listBusinessOrderByStarDesc').then(response => {
        this.businessArr = response.data.result;
      }).catch(error => {
        console.error(error);
      });
    } else if (this.sort == '销量降序') {
      this.$axios.post('BusinessController/listBusinessOrderBySalesDesc').then(response => {
        this.businessArr = response.data.result;
      }).catch(error => {
        console.error(error);
      });
    } else if (this.sort == '评星升序') {
      this.$axios.post('BusinessController/listBusinessOrderByStarAsc').then(response => {
        this.businessArr = response.data.result;
      }).catch(error => {
        console.error(error);
      });
    } else if (this.sort == '销量升序') {
      this.$axios.post('BusinessController/listBusinessOrderBySalesAsc').then(response => {
        this.businessArr = response.data.result;
      }).catch(error => {
        console.error(error);
      })
    }
  },
  mounted() {
    document.onscroll = () => {
      //获取滚动条位置
      let s1 = document.documentElement.scrollTop;
      let s2 = document.body.scrollTop;
      let scroll = s1 == 0 ? s2 : s1;
      //获取视口宽度
      let width = document.documentElement.clientWidth;
      //获取顶部固定块
      let search = this.$refs.fixedbox;
      //判断滚动条位置超过视口宽度的12%，搜索块变为固定定位
      if (scroll > width * 0.12) {
        search.style.position = 'fixed';
        search.style.left = 0;
        search.style.top = 0;
      }
      else {
        search.style.position = 'static'
      }
    }
  },
  //当切换到其他组件时就不需要document滚动条事件了
  destroyed() {
    document.onscroll = null;
  },
  components: {
    Footer,
    GoTopButton,
  },
  methods: {
    init() {
      // 2小时内关闭过，则不再展示
      let last_time = this.$getSessionStorage('last_time');
      let cur_time = Date.now();
      let delta_time = Math.ceil((cur_time - last_time) / 1000);
      if (delta_time < 3600 * 2)
        this.isShowed = 'false';
    },
    toLogin() {
      this.$router.push({
        path: '/login',
      })
    },
    closeActivity() {
      if (this.$getSessionStorage('last_time') == null)
        this.$setSessionStorage('last_time', Date.now())
      this.isShowed = 'false';
    },
    changeSort(sort) {
      if (sort == this.sort) {
        return;
      }
      else {
        this.$router.replace({
          path: '/',
          query: { sort: sort },
        })
        this.$router.go(0);
      }
    },

    toBusinessInfo(businessId) {
      this.$router.push({
        path: '/businessInfo',
        query: {
          businessId: businessId,
        }
      })
    },

    toSearch() {
      this.$router.push({
        path: '/search',
        query: {
          searchContent: this.searchContent,
        }
      });
    },

    toBusinessList(orderTypeId) {
      this.$router.push({
        path: '/businessList',
        query: {
          orderTypeId: orderTypeId
        }
      });
    },

    toRecommend() {
      this.$router.push({ path: '/404' });
    },

    toSuper() {
      this.$router.push({ path: '/404' });
    },
  }
}
</script>

<style scoped>
/**************************总容器**************************/
.wrapper {
  width: 100%;
}

/**************************index_header**************************/
.wrapper .index_header {
  width: 100%;
  height: 12vw;
  background-color: #0097ff;
  display: flex;
  align-items: center;
}

.wrapper .index_header .location-text {
  font-size: 4.5vw;
  font-weight: 700;
  color: #fff;
}

.wrapper .index_header .location-text .fa-map-marker {
  margin: 0 1vw 0 3vw;
}


.wrapper .index_header .location-text .fa-caret-down {
  margin: 0 0 0 1vw;
}

/**************************search**************************/
.wrapper .search {
  width: 100%;
  height: 13vw;
}

.wrapper .search .fixed {
  z-index: 1000;
  width: 100%;
  height: 13vw;
  background-color: #0097ff;
  display: flex;
  justify-content: center;
  align-items: center;
}

.wrapper .search .fixed .input-group {
  width: 90%;
  height: 9vw;
  margin-left: 5%;
  margin-right: 5%;
}

.wrapper .search .fixed .input-group input {
  background-color: #fff;
  border-radius: 2px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 3.5vw;
  color: #AEAEAE;
  font-family: "宋体";
  /*此样式让文本选中状态无效*/
  user-select: none;
}

.wrapper .search .fixed .input-group-text .fa-search {
  font-size: 3.5vw;
  width: 6vw;
}

/**************************点餐分类**************************/
.wrapper .foodtype {
  width: 100%;
  height: 48vw;

  display: flex;
  flex-wrap: wrap;

  justify-content: space-around;
  align-content: center;
}

.wrapper .foodtype li {
  width: 18vw;
  height: 20vw;

  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  user-select: none;
  cursor: pointer;
}

.wrapper .foodtype li img {
  width: 12vw;
  height: 12vw;
}

.wrapper .foodtype li p {
  font-size: 3.2vw;
  color: #666;
}

/**************************活动广告**************************/
.activity {
  height: 0;
}

#activity-img {
  width: 110%;
  height: 70%;
  position: fixed;
  top: 20vw;
  left: -1vw;
  cursor: pointer;
  z-index: 10000;

}

#activity-close-btn {
  color: black;
  position: fixed;
  top: 28vw;
  right: 10vw;
  cursor: pointer;
  font-size: 10vw;
  z-index: 10001;

}

#activity-content {
  color: #999;
  position: fixed;
  text-align: center;
  top: 47vw;
  right: 30vw;
  cursor: pointer;
  font-size: 5vw;
  z-index: 10001;

}

#activity-enter-btn {
  height: 9vw;
  padding-top: 1vw;
  font-size: 4.8vw;
  font-weight: bolder;
  background-color: rgb(175, 175, 175);
}

/**************************横幅广告**************************/

.wrapper .banner {
  width: 95%;
  margin: 0 auto;
  height: 29vw;

  background-image: url(../assets/img/index/品质套餐.jpg);
  background-repeat: no-repeat;
  background-size: 100% 100%;

  box-sizing: border-box;
  padding: 2vw 6vw;
}

.wrapper .banner h3 {
  font-size: 4.2vw;
  margin-bottom: 1.2vw;
}

.wrapper .banner p {
  font-size: 3.4vw;
  /* font-weight: bolder; */
  color: rgb(48, 47, 47);
  margin-bottom: 2.4vw;
}

.wrapper .banner a {
  font-size: 4.5vw;
  font-weight: bolder;
  color: #ed9e58;
  font-weight: 700;
}

/**************************超级会员**************************/
.wrapper .supermember {
  width: 95%;
  margin: 0 auto;
  height: 11.5vw;
  background-color: #FEECD1;
  margin-top: 1.3vw;
  border-radius: 2px;
  color: #644F1B;

  display: flex;
  justify-content: space-between;
  align-items: center;
}

.wrapper .supermember .left {
  display: flex;
  align-items: center;
  margin-left: 4vw;
  user-select: none;
}

.wrapper .supermember .left img {
  width: 6vw;
  height: 6vw;
  margin-right: 2vw;
}

.wrapper .supermember .left h3 {
  font-size: 4vw;
  margin-right: 2vw;
}

.wrapper .supermember .left p {
  font-size: 3vw;
}

.wrapper .supermember .right {
  font-size: 3vw;
  margin-right: 4vw;
  cursor: pointer;
}

/**************************推荐商家**************************/

.wrapper .recommend {
  width: 100%;
  height: 14vw;
  display: flex;
  justify-content: center;
  align-items: center;
}

.wrapper .recommend .recommend-line {
  width: 6vw;
  height: 0.2vw;
  background-color: #888;
}

.wrapper .recommend p {
  font-size: 4vw;
  margin: 0 4vw;
}

/**************************推荐方式部分**************************/

.focus {
  width: 21vw;
  height: 120%;
  padding: 0.5vw;
  text-align: center;
  background-color: rgb(220, 220, 220);
  border: solid, 2px, rgb(240, 240, 240);
  border-radius: 10px;

}

.card {
  width: 15vw;
  height: 6vw;
  background-color: rgb(240, 240, 240);
}

.wrapper .recommend-style {
  width: 100%;
  height: 5vw;
  margin-bottom: 2.5vw;

  display: flex;
  justify-content: space-around;
  align-items: center;
}

.wrapper .recommend-style li {
  font-size: 3.5vw;
  color: #555;
}

/**************************推荐商家部分**************************/
.wrapper .business {
  width: 96%;
  margin-left: 2%;
}

.wrapper .business li {
  width: 100%;
  margin-top: 1.9vw;
  box-sizing: border-box;
  padding: 2.5vw;
  user-select: none;
  border: solid 2px rgb(229, 233, 239);
  border-radius: 15px;


  display: flex;
}

.wrapper .business li:nth-last-child(1) {
  margin-bottom: 0;
}

.wrapper .business li img {
  width: 18vw;
  height: 18vw;
}

.wrapper .business li .business-info {
  width: 100%;
  box-sizing: border-box;
  padding-left: 3vw;
}

.wrapper .business li .business-info .business-info-h {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1vw;
}

.wrapper .business li .business-info .business-info-h h3 {
  font-size: 4vw;
  color: #333;
}

.wrapper .business li .business-info .business-info-h .business-info-like {
  width: 1.6vw;
  height: 3.4vw;
  background-color: #666;
  color: #fff;
  font-size: 4vw;
  margin-right: 4vw;
  display: flex;


  justify-content: center;
  align-items: center;
}

.wrapper .business li .business-info .business-info-star {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1vw;
  font-size: 3.1vw;
}

.wrapper .business li .business-info .business-info-star .business-info-star-left {
  display: flex;
  align-items: center;
}

.wrapper .business li .business-info .business-info-star .business-info-star-left .fa-star,
.fa-star-half-o,
.fa-star-o {
  color: #FEC80E;
  margin-right: 0.5vw;
}

.wrapper .business li .business-info .business-info-star .business-info-star-left p {
  color: #666;
  margin-left: 1vw;
}

.wrapper .business li .business-info .business-info-star .business-info-star-right {
  background-color: #0097FF;
  color: #fff;
  font-size: 2.4vw;
  border-radius: 2px;
  padding: 0 0.6vw;
}

.wrapper .business li .business-info .business-info-delivery {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1vw;

  color: #666;
  font-size: 3.1vw;
}

.wrapper .business li .business-info .business-info-explain {
  display: flex;
  align-items: center;
  margin-bottom: 1vw;
}

.wrapper .business li .business-info .business-info-explain div {
  border: solid 1px #DDD;
  font-size: 2.8vw;
  color: #666;
  border-radius: 3px;
  padding: 0 0.1vw;
}

.wrapper .business li .business-info .business-info-promotion {
  display: flex;

  justify-content: space-between;


  align-items: center;
  margin-bottom: 1.8vw;
}

.wrapper .business li .business-info .business-info-promotion .business-info-promotion-left {
  display: flex;
  align-items: center;
}

.wrapper .business li .business-info .business-info-promotion .business-info-promotion-left .business-info-promotion-left-icon {
  width: 4vw;
  height: 4vw;
  background-color: #70BC46;
  border-radius: 3px;
  font-size: 3vw;
  color: #fff;

  display: flex;
  justify-content: center;
  align-items: center;
}

.wrapper .business li .business-info .business-info-promotion .business-info-promotion-left p {
  color: #666;
  font-size: 3vw;
  margin-left: 2vw;
}

.wrapper .business li .business-info .business-info-promotion .business-info-promotion-right {
  display: flex;
  align-items: center;
  font-size: 2.5vw;
  color: #999;
}

.wrapper .business li .business-info .business-info-promotion .business-info-promotion-right p {
  margin-right: 2vw;
}

.hide {
  display: none;
}
</style>

